/**
 * 
 */
var _url = "service.do?s=adpmgt";
var dialog_title_new = "新建";
var dialog_title_edit = "编辑";

$(function(){
	// 广告位信息dialog初始化
	$("#ad_dialog").dialog({
		modal: true,
        autoOpen: false,
        width: 350,
        height: 230,
//        title: "新建广告位",
        show: true,
        hide: true,
        buttons: {
            "保存": function() {
			  	adSet();
            },
            "取消": function() {
            	$(this).dialog('close');
            	return false;
            }
        }
	});
	
	// 媒体dialog初始化
	$("#media_dialog").dialog({
		modal: true,
        autoOpen: false,
        width: 300,
        height: 150,
        title: "媒体",
//        show: true,
//        hide: true,
        buttons: {
            "确定": function() {
            	mediaSet();
            	return true;
            }
        }
	});
	
	// 新建广告位
	$("#ad_addNewItem").bind("click", function(){
		initialDialogSet();
		$("#ad_dialog").dialog( "option", "title", dialog_title_new );
		getMediaList();
		$("#dialogGenerateUrl").hide();
		$("#dialogUrl").attr("readonly", "readonly");
		$("#ad_dialog").dialog("open");
	});
	
	// 编辑广告位
	$(".adEdit").bind("click", function(){
		initialDialogSet();
		var obj = $(this);
		$("#ad_dialog").dialog( "option", "title", dialog_title_edit );
		$.post(_url,{op:"getAdConfiguration","adId":obj.parent().parent().children(".adpId").val()},function(data){
			var event = eval("(" + data + ")");
			if(event.returnCode == "success") {
				$("#dialogMedia").empty();
				$.each(event.result.mediaInfoList, function(index, obj){
					$("#dialogMedia").get(0).options.add(new Option(obj.mediaName, obj.mediaId));
				});
				$("#dialogMedia option").each(function(){
					if($(this).val() == event.result.mediaId)
					{
						$(this).attr("selected", "true");
					}
				})
				$("#dialogAd").val(event.result.adName);
				$("#dialogAd").attr("tag", event.result.adId);
				$("#dialogUrl").val(event.result.adUrl);
				
				$("#dialogGenerateUrl").bind("click", function(){
					$.post(_url, {op:"generateUrl","adId":event.result.adId}, function(data){
						var event = eval("(" + data + ")");
						if(event.returnCode == "success") {
							$("#dialogUrl").val(event.result);
						} else if(event.errorMessage != null && event.errorMessage != "") {
							alert(event.errorMessage);
						} else {
							alert("生成失败");
						}
					}, "html");
				});
			}
		},"html");
		$("#dialogMedia").bind("click", function(){this.ops=this.selectedIndex});
		$("#dialogMedia").bind("change", function(){this.selectedIndex=this.ops});
		$("#dialogNewMedia").hide();
		$("#dialogGenerateUrl").show();
		$("#dialogUrl").attr("readonly", "readonly");
		$("#ad_dialog").dialog("open");
	});
	
	// 管理媒体
	$("#dialogNewMedia").bind("click", function(){
		$("#media_dialog").dialog("open");
	});
	
	// 删除广告位
	$(".adDelete").bind("click", function(){
		var obj = $(this);
		if(confirm("删除将不可恢复，确定删除吗？", "提示")) {
			var data = {
					op:"deleteAd","adId":obj.parent().parent().children(".adpId").val()
			};
			deleteAd(data);
		}
	});
	
	// 每个广告位item鼠标动作
	$("ul.ad_item_even").mouseover(function(){
		$(this).removeClass("ad_item_even").addClass("ad_item_selected");
	});
	$("ul.ad_item_odd").mouseover(function(){
		$(this).removeClass("ad_item_odd").addClass("ad_item_selected");
	});
	$("ul.ad_item_even").mouseout(function(){
		$(this).removeClass("ad_item_selected").addClass("ad_item_even");
	});
	$("ul.ad_item_odd").mouseout(function(){
		$(this).removeClass("ad_item_selected").addClass("ad_item_odd");
	});
	$("#ad_addNewItem").mouseover(function(){
		$(this).removeClass("ad_addNewItem_out").addClass("ad_addNewItem_over");
	});
	$("#ad_addNewItem").mouseout(function(){
		$(this).removeClass("ad_addNewItem_over").addClass("ad_addNewItem_out");
	});
	
	// 复制到剪贴板，仅支持ie内核
	$(".copyUrl").bind("click", function(){
		var obj = $(this);
		$.post(_url, {op:"getUrl","adId":obj.parent().parent().children(".adpId").val()},function(data){
			var event = eval("(" + data + ")");
			if(event.returnCode == "success") {
				window.clipboardData.setData("Text",event.result);
				alert("已复制到剪贴板");
			} else if(event.errorMessage != null && event.errorMessage != "") {
				alert(event.errorMessage);
			} else {
				alert("复制失败");
			}
		}, "html");
	});
	
	// 生成链接
	$(".generateUrl").bind("click", function(){
		var obj = $(this);
		$.post(_url, {op:"generateUrl","adId":obj.parent().parent().children(".adpId").val()}, function(data){
			var event = eval("(" + data + ")");
			if(event.returnCode == "success") {
				obj.parent().html(
						"<span class=\"spanAdurl\">" + event.result + "</span>" +
					  	"<a class=\"copyUrl\" href=\"#\">复制链接</a>"
				);
				window.location.reload();
			} else if(event.errorMessage != null && event.errorMessage != "") {
				alert(event.errorMessage);
			} else {
				alert("生成失败");
			}
		}, "html");
	});
	
	
	// 获取所有广告位
//	$.post(_url,{op:"getAdList"},function(data){
//		
//	},"html");
	
});

/**
 * 新建或编辑保存
 * @returns
 */
var adSet = function(){
	var data;
  	if($("#ad_dialog").dialog( "option", "title" ) == dialog_title_new) {
		data = {
	  			op:"addAd",
	  			"mediaId":$("#dialogMedia").val(),
	  			"adName":$("#dialogAd").val()
	  	};
	} else if($("#ad_dialog").dialog( "option", "title" ) == dialog_title_edit) {
		data = {
				op:"editAd",
	  			"adId":$("#dialogAd").attr("tag"),
	  			"adName":$("#dialogAd").val()
	  	};
	}
  	$.post(_url, data, function(data){
  		var event = eval("(" + data + ")");
  		if(event.returnCode == "success") {
  			$("#ad_dialog").dialog("close");
  			window.location.reload();
  		} else if(event.errorMessage != null && event.errorMessage != "") {
			alert(event.errorMessage);
		} else {
			alert("保存失败");
		}
  	},"html");
}

var mediaSet = function() {
	$.post(_url,{op:"addMedia","mediaName":$("#media_dialog_mediaName").val()},function(data){
		var event = eval("(" + data + ")");
		if(event.returnCode == "success") {
			getMediaList();
		} else if(event.errorMessage != null && event.errorMessage != "") {
			alert(event.errorMessage);
		} else {
			alert("增加媒体失败");
		}
	},"html");
	$("#media_dialog").dialog("close");
}

/**
 * 初始化dialog
 * @returns
 */
var initialDialogSet = function() {
	$("#dialogMedia").empty();
	$("#dialogNewMedia").show();
	$("#dialogAd").val("");
	$("#dialogUrl").val("");
	$("#dialogGenerateUrl").show();
	$("#dialogMedia").unbind("click");
	$("#dialogMedia").unbind("change");
}

var getMediaList = function() {
	$.post(_url,{op:"getMediaList"},function(data){
		var event = eval("(" + data + ")");
		if(event.returnCode == "success") {
			$("#dialogMedia").empty();
			$.each(event.result, function(index, obj){
				$("#dialogMedia").get(0).options.add(new Option(obj.mediaName, obj.mediaId));
			});
		}
	},"html");
}

var deleteAd = function(data) {
	$.post(_url,data,function(data){
		var event = eval("(" + data + ")");
		if(event.returnCode == "success") {
			alert("删除成功");
		} else {
			alert("删除失败");
		}
		window.location.reload();
	}, "html");
}